package com.pwp.activity;

import java.util.ArrayList;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.agent.crm.base.BaseActivity;
import com.agent.crm.config.AppConfig;
import com.agent.crm.sechdule.receiver.RepeatingAlarm;
import com.agent.crm.view.MainActivity;
import com.agent.crm.view.NoteBookActivity;
import com.agent.crm.view.R;
import com.agent.crm.view.SearchActivity;
import com.pwp.borderText.BorderEditText;
import com.pwp.borderText.BorderTextView;
import com.pwp.constant.CalendarConstant;
import com.pwp.dao.ScheduleDAO;
import com.pwp.vo.ScheduleVO;

public class ScheduleInfoView extends BaseActivity {

	private LinearLayout layout = null;
	private ScrollView scrollView = null;
	private LinearLayout layoutParent = null;
	private BorderTextView textTop = null;
	private BorderTextView info = null;
	private BorderTextView date = null;
	private BorderTextView type = null;
	private BorderEditText editInfo = null;
	private ScheduleDAO dao = null;
	private ScheduleVO scheduleVO = null;
	
	private String scheduleInfo = "";    //日程信息被修改前的内容
	private String scheduleChangeInfo = "";  //日程信息被修改之后的内容
	private final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	private ArrayList<String> scheduleDate;
	
	private FrameLayout frameLayout= null;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

//		menu.add(1, menu.FIRST, menu.FIRST, "所有日程");
		menu.add(1, menu.FIRST+1, menu.FIRST+1,"添加日程");
		menu.add(1, menu.FIRST+2, menu.FIRST+2, "星期日程");
		menu.add(1, menu.FIRST+3, menu.FIRST+3,"本月日程");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        SharedPreferences 	preferences=getSharedPreferences("scheduleStatusMode", Context.MODE_PRIVATE);
		switch (item.getItemId()) {
//		case Menu.FIRST:
//			Intent intent = new Intent();
//			intent.setClass(ScheduleInfoView.this, ScheduleAll.class);
//			startActivity(intent);
//			break;
        case Menu.FIRST+1:
        	Intent intentAdd = new Intent();
			intentAdd.putExtra("operateType", "add");
	        intentAdd.putStringArrayListExtra("scheduleDate", scheduleDate);
	        intentAdd.setClass(ScheduleInfoView.this, ScheduleView.class);
	        startActivity(intentAdd);
	        finish();
	        break;
        case Menu.FIRST+2:
        	Editor eWeek=preferences.edit();
	        eWeek.putString("CalendarDefaultLunchMode", "WeekCalendar");
	        eWeek.commit();
        	Intent intentWeek = new Intent();
        	intentWeek.setClass(this, CalendarWeekActivity.class);
	        startActivity(intentWeek);
	        finish();
	        break;
        case Menu.FIRST+3:
        	Editor eMonth=preferences.edit();
	        eMonth.putString("CalendarDefaultLunchMode", "MonthCalendar");
	        eMonth.commit();
	        Intent intentMonth = new Intent();
	        intentMonth.setClass(ScheduleInfoView.this, CalendarActivity.class);
			startActivity(intentMonth);
	        finish();
	        break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	
	
	/**
	 * 显示日程所有信息
	 */
	public void handlerInfo(int scheduleID){
		BorderTextView date = new BorderTextView(this, null);
		date.setTextColor(Color.BLACK); 
		date.setBackgroundColor(Color.WHITE);
		date.setLayoutParams(params);
		date.setGravity(Gravity.CENTER_VERTICAL);
		date.setHeight(40);
		date.setPadding(10, 0, 10, 0);
		
		BorderTextView type = new BorderTextView(this, null);
		type.setTextColor(Color.BLACK); 
		type.setBackgroundColor(getResources().getColor(R.color.light_gray));
		type.setLayoutParams(params);
		type.setGravity(Gravity.CENTER);
		type.setHeight(40);
		type.setPadding(10, 0, 10, 0);
		type.setTag(scheduleID);
		
		final BorderTextView info = new BorderTextView(this, null);
		info.setTextColor(Color.BLACK); 
		info.setBackgroundColor(Color.WHITE);
		info.setGravity(Gravity.CENTER_VERTICAL);
		info.setLayoutParams(params);
		info.setPadding(10, 5, 10, 5);
		
		
		layout.addView(type);
		layout.addView(date);
		layout.addView(info);
		/*Intent intent = getIntent();
		int scheduleID = Integer.parseInt(intent.getStringExtra("scheduleID"));*/
		scheduleVO = dao.getScheduleByID(scheduleID);
		date.setText(scheduleVO.getScheduleDate());
		type.setText(CalendarConstant.sch_type[scheduleVO.getScheduleTypeID()]+"\t\t\t"+scheduleVO.getScheduleTitle());
		info.setText(scheduleVO.getScheduleContent());
		layout.setTag(scheduleID);
		layout.setOnClickListener(this);
		
//		//长时间按住日程类型textview就提示是否删除日程信息
//		type.setOnLongClickListener(new OnLongClickListener() {
//			
//			@Override
//			public boolean onLongClick(View v) {
//
//				final String scheduleID = String.valueOf(v.getTag());
//				
//				new AlertDialog.Builder(ScheduleInfoView.this)
//				.setTitle("删除日程")
//				.setMessage("确认删除")
//				.setPositiveButton("确认", new OnClickListener() {
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//
//						dao.delete(Integer.parseInt(scheduleID));
////						Intent intent1 = new Intent();
////						intent1.setClass(ScheduleInfoView.this, ScheduleAll.class);
////						startActivity(intent1);
//						layout.removeAllViews();
//						layout.postInvalidate();
//						layout.addView(frameLayout);
//						String[] scheduleIDs = dao.getScheduleByTagDate(Integer.parseInt(scheduleDate.get(0)), Integer.parseInt(scheduleDate.get(1)), Integer.parseInt(scheduleDate.get(2)));
//						//显示日程详细信息
//						for(int i = 0; i< scheduleIDs.length; i++){
//							handlerInfo(Integer.parseInt(scheduleIDs[i]));
//						}
//					}
//				}).setNegativeButton("取消", null).show();
//				
//				return true;
//			}
//		});
//		
//		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		final String scheduleID = String.valueOf(v.getTag());
		
		new AlertDialog.Builder(ScheduleInfoView.this)
        .setTitle("请选择操作类型")
        .setItems(R.array.schdule_operate_items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                /* User clicked so do some stuff */
                String[] items = getResources().getStringArray(R.array.schdule_operate_items);
//                new AlertDialog.Builder(ScheduleInfoView.this)
//                        .setMessage("You selected: " + which + " , " + items[which])
//                        .show();
                if(which==0){
                	//修改任务
                	Intent intentEdit = new Intent();
                	ScheduleVO scheduleVO = dao.getScheduleByID(Integer.parseInt(scheduleID));
                	intentEdit.putExtra("operateType", "edit");
                	intentEdit.putExtra("schedule", scheduleVO);
        			intentEdit.setClass(ScheduleInfoView.this, ScheduleView.class);
    		        startActivity(intentEdit);
    		        finish();
                }else{
                	
					//停止任务提示
					Intent intent = new Intent(ScheduleInfoView.this,
        					RepeatingAlarm.class);
            		PendingIntent sender = PendingIntent.getBroadcast(ScheduleInfoView.this,
            				Integer.parseInt(scheduleID), intent, 0);
        			// And cancel the alarm.
        			AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        			am.cancel(sender);
        			//删除任务
                	dao.delete(Integer.parseInt(scheduleID));
					layout.removeAllViews();
					layout.postInvalidate();
					layout.addView(frameLayout);
					String[] scheduleIDs = dao.getScheduleByTagDate(Integer.parseInt(scheduleDate.get(0)), Integer.parseInt(scheduleDate.get(1)), Integer.parseInt(scheduleDate.get(2)),3);
					//显示日程详细信息
					for(int i = 0; i< scheduleIDs.length; i++){
						handlerInfo(Integer.parseInt(scheduleIDs[i]));
					}
                }
            }
        }).show();
	}
	
	

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	protected View initCustomLayout() {
		// TODO Auto-generated method stub
		dao = new ScheduleDAO(this);
		
		Intent intent = getIntent();
		//scheduleID = Integer.parseInt(intent.getStringExtra("scheduleID"));
		//一个日期可能对应多个标记日程(scheduleID)
//		String[] scheduleIDs = intent.getStringArrayExtra("scheduleID");
		
		scheduleDate=intent.getStringArrayListExtra("scheduleDate");
        //final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 0, 0);
		layout = new LinearLayout(this); // 实例化布局对象
		scrollView=new ScrollView(this);
		LinearLayout.LayoutParams scrollViewpParams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		scrollViewpParams.weight=1.0f;
		scrollView.setLayoutParams(scrollViewpParams);
		layoutParent = new LinearLayout(this); // 实例化布局对象
		layoutParent.setLayoutParams(scrollViewpParams);
		layoutParent.setOrientation(LinearLayout.VERTICAL);
		layoutParent.setBackgroundColor(Color.WHITE);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setBackgroundResource(R.drawable.schedule_bk);
//		layout.setBackgroundColor(Color.GRAY);
		layout.setLayoutParams(params);
		LayoutInflater inflaterHead =LayoutInflater.from(this);
		frameLayout =new FrameLayout(this);
		inflaterHead.inflate(R.layout.header_banner, frameLayout);
		frameLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
		Button leftAddBtn=(Button)frameLayout.findViewById(R.id.bt_add_contacts);
//		leftAddBtn.setLayoutParams(new LinearLayout.LayoutParams(50,50));
////		android.widget.FrameLayout.LayoutParams btnParams = (FrameLayout.LayoutParams)leftAddBtn.getLayoutParams();
//		leftAddBtn.setGravity(Gravity.LEFT);
////		leftAddBtn.setGravity(Gravity.CENTER);
//		leftAddBtn.setBackgroundResource(R.drawable.create_new_group);
//		leftAddBtn.setPadding(10, 0, 0, 0);
////		leftAddBtn.setWidth(45);
////		leftAddBtn.setHeight(45);
		leftAddBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intentAdd = new Intent();
				intentAdd.putExtra("operateType", "add");
		        intentAdd.putStringArrayListExtra("scheduleDate", scheduleDate);
		        intentAdd.setClass(ScheduleInfoView.this, ScheduleView.class);
		        startActivity(intentAdd);
		        finish();
			}
		});
		Button rightBtn=(Button)frameLayout.findViewById(R.id.title_bt_setting);
		rightBtn.setVisibility(View.GONE);
		TextView titleText=(TextView)frameLayout.findViewById(R.id.title_txt);
//		textTop = new BorderTextView(this, null);
//		textTop.setTextColor(Color.BLACK); 
//		textTop.setBackgroundResource(R.drawable.head);
////		textTop.setBackgroundColor(getResources().getColor(R.color.skyblue));
		titleText.setText(scheduleDate.get(0)+"年"+scheduleDate.get(1)+"月"+scheduleDate.get(2)+"日——"+"日程详情");
		titleText.setTextSize(18.0f);
//		textTop.setHeight(50);
//		textTop.setGravity(Gravity.CENTER);
//		
//		frameLayout.addView(textTop);
//		frameLayout.addView(leftAddBtn);
			
		editInfo = new BorderEditText(ScheduleInfoView.this, null);
		editInfo.setTextColor(Color.BLACK); 
		editInfo.setBackgroundColor(Color.WHITE);
		editInfo.setHeight(200);
		editInfo.setGravity(Gravity.TOP);
		editInfo.setLayoutParams(params);
		editInfo.setPadding(10, 5, 10, 5);
		
//		layout.addView(textTop);
		layout.addView(frameLayout);
		
		String[] scheduleIDs = dao.getScheduleByTagDate(Integer.parseInt(scheduleDate.get(0)), Integer.parseInt(scheduleDate.get(1)), Integer.parseInt(scheduleDate.get(2)),3);
		//显示日程详细信息
		for(int i = 0; i< scheduleIDs.length; i++){
			handlerInfo(Integer.parseInt(scheduleIDs[i]));
		}
		scrollView.addView(layout);
		LinearLayout bottomMenu=new LinearLayout(this);
		LayoutInflater inflater =LayoutInflater.from(this);
		inflater.inflate(R.layout.bottom_menu, bottomMenu);
		Button btn_Contact =(Button)bottomMenu.findViewById(R.id.contact);
		Button btn_carlendar=(Button)bottomMenu.findViewById(R.id.schedule_and_carlendar);
		btn_carlendar.setBackgroundResource(R.drawable.home_btn_bg_d);
		Button btn_search=(Button)bottomMenu.findViewById(R.id.search_contact_schedule);
		Button note_book=(Button)bottomMenu.findViewById(R.id.note_book);
		Button btn_attention = (Button)bottomMenu.findViewById(R.id.attention_contact);
		btn_Contact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent_main = new Intent();
				intent_main.setClass(ScheduleInfoView.this, MainActivity.class);
	            startActivity(intent_main);
			}
		});
		btn_search.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent_search = new Intent();
	            intent_search.setClass(ScheduleInfoView.this, SearchActivity.class);
	            startActivity(intent_search);
			}
		});
		note_book.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent noteIntent = new Intent();
	            noteIntent.setClass(ScheduleInfoView.this, NoteBookActivity.class);
	            startActivity(noteIntent);
			}
		});
		btn_attention.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Intent aatentionIntent = new Intent();
		            aatentionIntent.setClass(ScheduleInfoView.this, ContactAttentionActivity.class);
		            startActivity(aatentionIntent);
			}
		});

		layoutParent.addView(scrollView);
		layoutParent.addView(bottomMenu);
		return layoutParent;
	}

	@Override
	protected void initViewCmp() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initAsycnData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void changeDefaultSetting() {
		// TODO Auto-generated method stub
		customLayoutFlag=true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dao.destoryDB();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		int count=layout.getChildCount();
		if(count > 1){
			AppConfig.backPress=1;
		}else{
			AppConfig.backPress=2;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	
}
